{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "73bd968b-d970-4a05-94ef-4e7abf990827",
   "metadata": {},
   "source": [
    "Chapter 04\n",
    "\n",
    "# 无放回排列\n",
    "Book_3《数学要素》 | 鸢尾花书：从加减乘除到机器学习 (第二版)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "18c5e2e8-0899-4890-a87f-ce1e3a9ec2b7",
   "metadata": {},
   "source": [
    "该代码使用`itertools`库生成集合$\\{\"A\", \"B\", \"C\"\\}$中所有包含两个元素的排列。排列不同于组合，考虑了元素的顺序，因此对于选择的每一对元素，顺序不同会产生不同的排列结果。代码调用`itertools.permutations`函数，生成不重复的排列，依次输出每种排列情况。\n",
    "\n",
    "对于集合$\\{\"A\", \"B\", \"C\"\\}$，选择两个元素的排列情况为：\n",
    "\n",
    "$$\n",
    "(A, B), (A, C), (B, A), (B, C), (C, A), (C, B)\n",
    "$$\n",
    "\n",
    "排列的数量计算公式为：\n",
    "\n",
    "$$\n",
    "P(3, 2) = 3 \\times 2 = 6\n",
    "$$\n",
    "\n",
    "这意味着在三元素集合中选择两个元素的排列总数为$6$，且每种排列都区分了顺序。该代码展示了生成特定大小排列的方法，适用于需要考虑元素顺序的排列问题和组合数学中的应用。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "f0b88a5a-bcd7-4233-ac48-0f5759f8070e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "('A', 'B')\n",
      "('A', 'C')\n",
      "('B', 'A')\n",
      "('B', 'C')\n",
      "('C', 'A')\n",
      "('C', 'B')\n"
     ]
    }
   ],
   "source": [
    "## 从3个字母中排列出所有包含2个字母的排列\n",
    "import itertools\n",
    "\n",
    "letters = \"ABC\"  # 定义字母集\n",
    "\n",
    "per = itertools.permutations(letters, 2)  # 生成包含2个字母的排列\n",
    "\n",
    "for val in per:\n",
    "    print(val)  # 输出每一个排列\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "85a80909-2aac-49ed-bb7a-f8cc6b80ee7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ecd322f4-f919-4be2-adc3-69d28ef25e69",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
